<?php
	include("template.inc");
	require_once("scripts/global_2.php");
	require_once("scripts/constraint.php");
	require_once("scripts/global.php");
	require_once("scripts/EKUser.php");
	require_once("scripts/EKDocument.php");
	
class Document_Handler{
  private $result;
  
  private $canView= false;
  private $canEdit= false;
  private $canApprove= false;
  
  private $id = "";
  private $status = "";
  
  private $title = "";
  private $creatorId = "";
  private $creator = "";
  private $fatherId = "";
  private $father = "";
  private $updateTime = "";
  
  
  private $currentElement = "";
  
  function get_result(){
  	return $this->result;
  }
  function start_element($parser, $name, $attrs){
  	
     if ($name == "documentRoot"){
        $this->result = $this->result."<h3>Documents</h3>\n".
           "<table width='100%'><tr>".
            "<th>Title</th>".
            "<th>Creator</th>".
        	"<th>Father document</th>".
        	"<th>Updated</th>".
            "<th>Status</th>".
            "<th>Operations</th></tr>";
     }else if ($name == "document"){
        if ($attrs['enabled'] == "true" ){
           $this->canView = true;
        }
        if ($attrs['status'] == "pending" ){
           $this->canEdit = true;
           if ($_SESSION["usertype"] == 1)
           {
           		$this->canApprove = true;
           }
        }
        $this->id = $attrs['id'];
        $this->status = $attrs['status'];
     }
     $this->currentElement = $name;
  }
  
  function end_element($parser, $name){
  	
  	 if ($name == "documentRoot"){
       $this->result = $this->result."\n</table>";
     }else if($name == "document" && $this->canView){
     	$this->result = $this->result."\n<tr><td>.$this->title.</td>".
     	"<td><a href=\"user.php?userid=".$this->creatorId."&method=v\" > ".$this->creator."</a></td>";
     	if($this->fatherId == "null")
     	{
     		$this->result = $this->result."<td>none</td>";
     	}else{
     		$this->result = $this->result."<td><a name=\"".$this->id."\" href=\"#".$this->fatherId."\" >".$this->father."</a></td>";
     	}
        $this->result = $this->result."<td>".$this->updateTime."</td>".
     	"<td>".$this->status."</td>".
        "<td>".
          "\n<a href=\"docs.php?id=".$this->id."&method=v\">View</a>&nbsp;";
     	if($this->canEdit)
     	{
     		$this->result = $this->result."\n<a href=\"docs.php?id=".$this->id."&method=e\">Edit</a>&nbsp;";
     	}
     	$this->result = $this->result."\n<a href=\"docs.php?fatherId=".$this->id."&method=c\">Add child</a>&nbsp;";
     	if($this->canApprove)
     	{
     		$this->result = $this->result."\n<a href=\"document_approve.php?id=".$this->id."\">Approve</a>&nbsp;";
     	}
     	$this->result = $this->result."\n</td>";

        $this->canView= false;
        $this->canEdit= false;
        $this->canApprove= false;
        $this->id = "";
        $this->status = "";
        $this->title = "";
        $this->creatorId = "";
        $this->creator = "";
        $this->fatherId = "";
        $this->father = "";
        $this->updateTime = "";
  		$this->currentElement = "";
     }
     
  } 
  function chars($parser, $chars){
     if ($this->canView){
         if ($this->currentElement == "title" && chop($chars)!=""){
            $this->title = $chars;
         }else if ($this->currentElement == "creatorId"&& chop($chars)!=""){
         	$this->creatorId = $chars;
         	if($_SESSION["userid"] != $chars)
         		$this->canEdit = false;
            $this->creator = $this->creator.EKUser::getAttributeById($chars, "name");
         }else if($this->currentElement == "fatherId"&& chop($chars)!=""){
         	$this->fatherId = $chars;
         	if($chars != "null")
         	{
         		$aaaa = new EKDocument($chars);
         		$this->father = $aaaa->getTitle();
         	}
         }else if($this->currentElement == "updateTime" && chop($chars)!=""){
         	$this->updateTime = date("Y-m-d H:i:s",$chars);
         }
      }
  } 
}
	
function get_document_list()
{
	$handler = new Document_Handler();
	$doc_parser = xml_parser_create();
	xml_set_object($doc_parser, $handler); 
	xml_set_element_handler($doc_parser,
                           "start_element",
                           "end_element");
    xml_set_character_data_handler($doc_parser, "chars");
    xml_parser_set_option($doc_parser,
                        XML_OPTION_CASE_FOLDING, 0);

	$xmlfile = SERVERLOCATION."data/EKDocument.xml";

    $file_to_parse = fopen($xmlfile, "r");
    if (!$file_to_parse) die("Can't open XML file.");

    while($data = fread($file_to_parse, 4096)){
          xml_parse($doc_parser, $data, feof($file_to_parse));
    }
    
    return $handler->get_result();
}
	
	//generate template output variables
	$template = new Template();
	$template->set_file("document_list","template/document_list.html");
		
	//set page title
	$template->set_var("pageTitle",SITENAME);
		
	//set document title
	$template->set_var("title", "Document List");
		
	//set document list
	$template->set_var("documentList", get_document_list());
	$template->parse("output","document_list");
	$template->p("output");
?>